<?php 
namespace Home\Controller;

// use \Think\Controller;

class PublicController extends HomeController
{	
	private $_model = null; //数据库操作类

	public function _initialize()
	{
		$this->_model = D('user');
		$this->_score = M('score');
		// var_dump($this->_model);exit;
	}

	//ajax操作
	public function doajax()
	{
		$phone = I('phone');
		$userpass = md5(I('userpass'));
		$user = D('user');
		$data = $user->where(array('phone'=>$phone,'userpass'=>$userpass))->find();

        $this->ajaxReturn($data);


	}

	public function doregajax() 
	{
		$phone = I('post.phone');
		$user = D('user');
		$data = $user->where(array('phone'=>$phone))->find();

        $this->ajaxReturn($data);
	}

	//验证码
	public function yzm()
	{
		$Verify = new \Think\Verify();
		$Verify->imageW =200;
		$Verify->length = 4;
		$Verify->entry();
	}
	//执行登陆
	public function dologin(){
		//接收用户名和密码
		$phone = I('post.phone');
		$userpass = I('post.userpass');
		//验证
		$user = D('user');

		// var_dump($user);
		
		$status = $user->field('status')->where(array('phone'=>$phone))->find();
		$data = $user->where(array('phone'=>$phone))->find();
		//判断用户的状态
		if($status['status'] == 1){
			$this->error('你的账户已冻结请联系管理员');
			exit;
		}
		
		$_SESSION['home_user'] = $data;
		if (!$data) {
			$this->error('手机号不存在！');
			exit;
		}
		//验证密码
		if ($data['userpass'] != md5($userpass)) {
			$this->error('密码不正确');
			exit;
		}

		$this->success('登录成功');
			exit;				
	}

	public function doadd()
	{

		if (!$this->_model->create()) {
			$this->error($this->_model->getError());
			exit;
		}
		$data =  $this->_model->create();
		// var_dump($data['phone']);exit;
		//返回自增ID
		if($this->_model->add()) {
			// //在注册的成功的时候添加积分表
			// $m = $this->_model->field('id')->where('phone='.$data['phone'])->find();
			// $uid['uid'] = $m['id'];
			// $this->_score->add($uid);
			$this->success('恭喜注册成功,正在返回登录页面',U('Index/index'),3);
		} else {
			$this->error('添加失败');
		}
	}

	//退出登陆
	public function logout()
	{
		//清空session
		unset($_SESSION['home_user']);
		//跳转
		$this->success('退出成功',U("Index/index"));
	}
}